package service

import (
	"errors"

	"sieve_admin_server_userpass/pkg/im3/util/timer"
	"sieve_admin_server_userpass/pkg/json"
)

func AddTimer(ts int64, svc string, api string, data interface{}, repeat *timer.Repeat) (int64, error) {
	j, err := json.Marshal(data)
	if err != nil {
		return 0, err
	}
	var id int64
	err = ReqSvc("timer", "Add", map[string]interface{}{
		"Ts":     ts,
		"Url":    "svc://" + svc + "/" + api + "?" + string(j),
		"Repeat": repeat,
	}, &id)
	return id, err
}

func RemoveTimer(id int64) error {
	ret := ""
	if err := ReqSvc("timer", "Remove", map[string]int64{"Id": id}, &ret); err != nil {
		return err
	}
	if ret != "OK" {
		return errors.New("failed")
	}
	return nil
}
